package androidx.camera.core.impl;

import androidx.annotation.NonNull;
import androidx.camera.core.CameraInfo;
import androidx.camera.core.CameraSelector;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import static androidx.camera.core.CameraSelector.LENS_FACING_BACK;
import static androidx.camera.core.CameraSelector.LENS_FACING_FRONT;

public class LensFacingCameraFilterShadow extends LensFacingCameraFilter {
    public LensFacingCameraFilterShadow(@CameraSelector.LensFacing int lensFacing) {
        super(lensFacing);
    }

    @NonNull
    @NotNull
    @Override
    public List<CameraInfo> filter(@NonNull @NotNull List<CameraInfo> cameraInfos) {
        List<CameraInfo> result = super.filter(cameraInfos);
        if (!result.isEmpty()) return result;
        switch (getLensFacing()) {
            case LENS_FACING_FRONT: {
                for (CameraInfo info : cameraInfos) {
                    CameraInfoInternal cii = null;
                    if (info instanceof CameraInfoInternal) cii = (CameraInfoInternal) info;
                    if (cii == null) break;
                    if (cii.getCameraId().equals(String.valueOf(LENS_FACING_FRONT))) {
                        result.add(cii);
                        break;
                    }
                }
                break;
            }
            case LENS_FACING_BACK: {
                for (CameraInfo info : cameraInfos) {
                    CameraInfoInternal cii = null;
                    if (info instanceof CameraInfoInternal) cii = (CameraInfoInternal) info;
                    if (cii == null) break;
                    if (cii.getCameraId().equals(String.valueOf(LENS_FACING_BACK))) {
                        result.add(cii);
                        break;
                    }
                }
                break;
            }
        }
        return result;
    }
}
